#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef int QElemType_t;

typedef struct Queue{
    QElemType_t *queue;
    int first;
    int last;
    int maxSize;
}SeqQue;

SeqQue *manager;

void queue_init(int maxSize)
{
    //1 申请队列管理结构体内存空间
    manager = (SeqQue *)malloc(sizeof(SeqQue));
    if(manager == NULL){
        printf("申请内存失败\n");
        return;
    }
    //2 初始化队列
    manager->first = 0;
    manager->last = 0;
    manager->maxSize = maxSize;
    //3 申请队列元素空间
    manager->queue = (QElemType_t *)malloc(sizeof(QElemType_t)*maxSize);
}
//判满
bool isFull()
{
    return (manager->last + 1) % manager->maxSize == manager->first;
}
//尾插 -入队
void push(QElemType_t data)
{
    //1 判断队列是否已满
    if(isFull()){
        printf("队列已满\n");
        return;
    }

    manager->queue[manager->last] = data;
    manager->last = (manager->last + 1) % manager->maxSize;
    
}
//判空
bool isEmpty()
{
    return manager->first == manager->last;
}
//头删 -出队
void pop(QElemType_t *data)
{
    //1 判断队列是否为空
    if(isEmpty()){
        printf("队列为空\n");
        return;
    }
    *data = manager->queue[manager->first];
    manager->first = (manager->first + 1) % manager->maxSize;
}






int main()
{
    queue_init(10);
    
    push(1);
    push(2);
    push(3);
    push(4);
    push(5);

    while (1)
    {
        QElemType_t data;
        pop(&data);

        printf("%d\n",data);

            
        if(isEmpty()){
            break;
        }
    }
    


    return 0;
}